﻿package Interface.Top100Classes
{
	import caurina.transitions.Tweener;	
	
	import flash.display.Sprite;	
	
	public class TopContainer extends Sprite
	{
		private const CItemPadding:int = 31;		
		private const CItemsPerPage:int = 7;
		private const CPageSize:int = 217;
		private const CTweenTime:int = 1;
		private const CY:int = 50;	
		
		private var FPagesCount:int;
		private var FItemsCount:int;
		private var FCurrentPage:int;
		private var FItems:Vector.<TopLabel>;
		
		public function TopContainer():void
		{
			FItems = new Vector.<TopLabel>;
			FPagesCount = 0;
			FCurrentPage = 0;
			FItemsCount = 0;			
		}
		
		public function MoveUp():void
		{
			if(FCurrentPage > 0)
			{
				FCurrentPage--;			
				Tweener.addTween(this, {y:GetY, time:CTweenTime, transition: "easeOutElastic"});
			}
		}
		
		public function MoveDown():void
		{				
			if(FCurrentPage < FPagesCount)			
			{								
				FCurrentPage++;				
				Tweener.addTween(this, {y:GetY, time:CTweenTime, transition: "easeOutElastic"});
			}
		}
		
		public function AddItem(ALabel:TopLabel):void
		{
			addChild(ALabel);
			ALabel.y = CItemPadding * FItemsCount;
			FItemsCount++;
			FItems.push(ALabel);
			FPagesCount = Math.floor(FItemsCount / CItemsPerPage);
			if(FItemsCount % CItemsPerPage <= 0)
				FPagesCount = Math.max(FPagesCount - 1, 0);
		}
		
		public function Clear():void
		{			
			for(var i in FItems)
			{
				removeChild(FItems[i]);
			}			
			FItems = new Vector.<TopLabel>;
			Update();
			FCurrentPage = 0;
			y = GetY;			
		}
		
		private function Update():void
		{
			for(var i in FItems)
			{				
				FItems[i].y = CItemPadding * i;				
			}
			FItemsCount = FItems.length;
			FPagesCount = Math.floor(FItemsCount / CItemsPerPage);
			if(FItemsCount % CItemsPerPage <= 0)
				FPagesCount = Math.max(FPagesCount - 1, 0);
		}
		
		private function get GetY():Number
		{
			return CY - FCurrentPage * CPageSize;
		}
				
		public function get IsFirstPage():Boolean
		{
			return FCurrentPage == 0;
		}
		
		public function get IsLastPage():Boolean
		{
			return FCurrentPage == FPagesCount;
		}
		
		public function get PagesCount():int
		{
			return FPagesCount;			
		}
	}	
}
